package com.j2eefast.web.common;

import cn.hutool.extra.mail.MailAccount;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.j2eefast.common.config.service.SysConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.List;

@Component
public class MailUtil {

    @Autowired
    private SysConfigService sysConfigService;

    private final static String USERNAME = "jumucreport@163.com";
    private final static String PASSWORD = "FUDXKVGGCHKSWGTH";

    private final static String KEY_EMAIL_LIST = "REPORT_EMAIL_LIST";

    public void sendReportMail(String subject,String filePath) {

        if(StringUtils.isEmpty(filePath)) return;

        File file = new File(filePath);
        if(!file.exists()) return;

        MailAccount account = new MailAccount();
        account.setHost("smtp.163.com");
        account.setPort(25);
        account.setAuth(true);
        account.setFrom(USERNAME);
        account.setUser(USERNAME);
        account.setPass(PASSWORD);

        String value = sysConfigService.getParamValue(KEY_EMAIL_LIST);
        Iterable<String> emailList = Splitter.on("\r\n").split(value);

        cn.hutool.extra.mail.MailUtil.send(account, Lists.newArrayList(emailList),subject,"",false,file);
    }
}
